package com.tintintech.sendmail.util;

import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class Json {
    
    public final String encode(Map<String, String> obj)
    {
        Gson gson = new Gson();
        return gson.toJson(obj);
    }
    
    public final String encode(List<String> obj)
    {
        Gson gson = new Gson();
        return gson.toJson(obj);
    }
    
    public final List<Map<String, String>> decodeForList(String s)
    {//List<Map<String, String>> | Map<String, String>
        try {
            Gson gson = new Gson();
            Type typeOfList = new TypeToken<List<Map<String, String>>>(){}.getType();
            return gson.fromJson(s, typeOfList);
        } catch (Exception e){
            
        }
        return null;
    }
    
    public final Map<String, String> decodeForMap(String s)
    {
        try {
            Gson gson = new Gson();
            Type typeOfMap = new TypeToken<Map<String, String>>(){}.getType();
            return gson.fromJson(s, typeOfMap);
        } catch (Exception e) {
            System.out.println("json-decode-err");
        }
        return null;
    }
}
